package com.cqu.lpss.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static String signKey = "liuyawen";
    private static Long expire = 43200000L;
    /**
    * @Description:jwt令牌生成
    * @Param:[claims]
    * @Return: java.lang.String
    * @Date: 2024/4/8
    * @Author:liuyawen
    */

    public static  String generateJwt(Map<String,Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256,signKey)
                .setExpiration(new Date(System.currentTimeMillis()+expire))
                .compact();
        return jwt;
    }
    /**
    * @Description:令牌解析
    * @Param:[jwt]
    * @Return: io.jsonwebtoken.Claims
    * @Date: 2024/4/8
    * @Author:liuyawen
    */

    public static Claims parseJWT(String jwt){

        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
